<?php
/**
 * Created by PhpStorm.
 * User: wangjie
 * Date: 2020/07/27
 * Time: 下午5:13
 */
namespace App\Api\Controllers\Lakala;

use App\Api\Controllers\BaseController;
use App\Common\PaySuccessAction;
use App\Models\Order;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Schema;
use App\Api\Controllers\Cmbc\BaseController as base;

class NotifyController extends BaseController{

    public function pay_notify_url(Request $request)
    {
        try {
            $data = $request->all();

            Log::channel("lakala")->info("notify", $data);

            if (isset($data['requestJson']) && !empty($data['requestJson'])) {
                $data = json_decode($data['requestJson'], true);
            }
            $verify = false;
//            var_dump($data['body']['outTradeNo']);
            if (isset($data['body']['outTradeNo']) && !empty($data['body']['outTradeNo'])) {
                $out_trade_no = $data['body']['outTradeNo'];

                $day = date('Ymd', time());
                $table = 'orders_' . $day;
                if (Schema::hasTable($table)) {
                    $order = DB::table($table)->where('out_trade_no', $out_trade_no)->first();
                } else {
                    $order = Order::where('out_trade_no', $out_trade_no)->first();
                }

//                $config = DB::table('lakala_configs')->where('config_id', $order->config_id)->first();

                $key = DB::table('lakala_stores')->where('store_id', $order->store_id)->value('key');


                $base = \App\Api\Controllers\Lakala\BaseController::getInstance();
                $verify  = $base->verify($data, $key);
                if ($verify) {
                    if (isset($data['body']['rspStatus']) && $data['body']['rspStatus'] == 'S') {
                        //订单存在
                        if ($order) {
                            //系统订单未成功
                            if ($order->pay_status == '2') {

                                $trade_no = isset($data['body']['upChannelTradeNo']) ? $data['body']['upChannelTradeNo'] : '';
                                $buyer_user_id = '';

                                if (!empty($data['body']['wxUniqueInfo']['subOpenid'])) {
                                    $buyer_user_id = $data['body']['wxUniqueInfo']['subOpenid'];
                                }

                                if (!empty($data['body']['aliUniqueInfo']['buyerUserId'])) {
                                    $buyer_user_id = $data['body']['aliUniqueInfo']['buyerUserId']??"";
                                }

                                $pay_time = $data['body']['timeEnd'];
                                $buyer_pay_amount = $data['body']['totalAmount'];

                                $in_data = [
                                    'status' => '1',
                                    'pay_status' => 1,
                                    'pay_status_desc' => '支付成功',
                                    'trade_no' => $trade_no,
                                    'pay_time' => $pay_time,
                                    'buyer_id' => $buyer_user_id,
                                    'buyer_pay_amount' => $buyer_pay_amount,
                                ];

                                $this->update_day_order($in_data, $out_trade_no);

                                if (strpos($out_trade_no, 'scan')) {

                                } else {
                                    //支付成功后的动作
                                    $data = [
                                        'ways_type' => $order->ways_type,
                                        'company' => $order->company,
                                        'ways_type_desc' => $order->ways_type_desc,
                                        'source_type' => '80001', //返佣来源
                                        'source_desc' => '联厚支付', //返佣来源说明
                                        'total_amount' => $order->total_amount,
                                        'out_trade_no' => $order->out_trade_no,
                                        'other_no' => $order->other_no,
                                        'rate' => $order->rate,
                                        'fee_amount' => $order->fee_amount,
                                        'merchant_id' => $order->merchant_id,
                                        'store_id' => $order->store_id,
                                        'user_id' => $order->user_id,
                                        'config_id' => $order->config_id,
                                        'store_name' => $order->store_name,
                                        'ways_source' => $order->ways_source,
                                        'pay_time' => $pay_time,
                                        'device_id' => $order->device_id,
                                    ];
                                    PaySuccessAction::action($data);
                                }
                                echo '{"code":"0000","message":"成功"}';
                            }
                        }
                    }
                }
            }
        } catch (\Exception $ex) {
            echo $ex->getMessage();
        }
    }


    

}
